let MongoClient = require('mongodb').MongoClient
let {url, dbname} = require('./conf/config')

class Db {
    // 单例, 解决多次实例化实例不共享问题
    static getInstance () {
        if (!Db.instance) {
            Db.instance = new Db()
        } 
        return Db.instance
        
    }
    constructor () {
        // 解决多次链接问题
        this.dbClient = this.connect()
        
    }
    
    connect () {
        return new Promise((resolve, reject) => {
            if (!this.dbClient) {
                MongoClient.connect(url, (err, client) => {
                    if (err) reject(err)
                    console.log('数据库链接成功...')
                    this.dbClient = client
                    resolve(this.dbClient)
                })
            } else {
                resolve(this.dbClient)
            }
        })
    }

    find (coll, json) {
        return new Promise((resolve, reject) => {
            this.connect().then(client => {
                let db = client.db(dbname)
                let result = db.collection(coll).find(json)
                result.toArray((err, item) => {
                    if (err) reject(err)
                    resolve(item)
                })
            })
        })
    }

    update (collectionName, json1, json2) {
        return new Promise((resolve, reject) => {
            this.connect().then((client) => {
                let db = client.db(dbname)
                let data = db.collection(collectionName).updateOne(json1, {
                    $set: json2
                }, (err, data) => {
                    if (err) reject(err)
                    resolve(data)
                })
            })
        })
    }

    insert (collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then(client => {
                let db = client.db(dbname)
                db.collection(collectionName).insertOne(json, (err, data) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(data)
                    }
                })
            })
        })
    }

    remove (collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then(client => {
                let db = client.db(dbname)
                db.collection(collectionName).remove(json, (err, data) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(data)
                    }
                })
            })
        })
    }
}

module.exports = Db.getInstance()


// let mongo = Db.getInstance()

// setTimeout(function() {
//     console.time('start1')
//     mongo.find('users', {'name': 'mystic'}).then(data => {
//         // console.log(data)
//         console.timeEnd('start1')
//     })
// }, 1000)

// setTimeout(function() {
//     console.time('start2')
//     mongo.find('users', {'name': 'mystic'}).then(data => {
//         // console.log(data)
//         console.timeEnd('start2')
//     })
// }, 3000)

// let mongo2 = Db.getInstance()

// setTimeout(function() {
//     console.time('start3')
//     mongo2.find('users', {'name': 'mystic'}).then(data => {
//         // console.log(data)
//         console.timeEnd('start3')
//     })
// }, 5000)

// setTimeout(function() {
//     console.time('start4')
//     mongo2.find('users', {'name': 'mystic'}).then(data => {
//         // console.log(data)
//         console.timeEnd('start4')
//     })
// }, 8000)